package com.qzdsoft.erpcloud.service.act.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.act.CenterCompanyProduct;
import com.qzdsoft.erpcloud.domain.act.CenterProduct;
import com.qzdsoft.erpcloud.mapper.act.CenterCompanyProductMapper;
import com.qzdsoft.erpcloud.mapper.act.CenterProductMapper;
import com.qzdsoft.erpcloud.service.act.CenterCompanyProductService;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;

/**
 * Created by Yang
 */
@Service
public class CenterCompanyProductServiceImpl implements CenterCompanyProductService {

    @Autowired
    private CenterCompanyProductMapper actCenterCompanyProductMapper;

    @Autowired
    private CenterProductMapper actCenterProductMapper;

    @Override
    public CenterCompanyProduct selectById(Integer id) {
        return actCenterCompanyProductMapper.selectByPrimaryKey(id);
    }

    /**
     * 保存与更新已购买产品
     * 
     */
    @Override
    public Response<String> saveOrUpd(CenterCompanyProduct actCenterCompanyProduct) {
        if(null != actCenterCompanyProduct.getId()){
            CenterCompanyProduct upd = selectById(actCenterCompanyProduct.getId());
            if(null != actCenterCompanyProduct.getBegin_time()){
                upd.setBegin_time(actCenterCompanyProduct.getBegin_time());
            }
            if(null != actCenterCompanyProduct.getEnd_time()){
                upd.setBegin_time(actCenterCompanyProduct.getEnd_time());
            }
            upd.setRemark(actCenterCompanyProduct.getRemark());
            actCenterCompanyProductMapper.updateByPrimaryKey(upd);
        } else {
            CenterProduct actCenterProduct = actCenterProductMapper.selectByPrimaryKey(actCenterCompanyProduct.getProduct_id());
            actCenterCompanyProduct.setProduct_name(actCenterProduct.getName());
            actCenterCompanyProductMapper.insert(actCenterCompanyProduct);
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }

    @Override
    /**
     * 根据id删除已购买商品
     * 
     */
    public Response<String> del(Integer id) {
//        CenterCompanyProduct actCenterCompanyProduct = selectById(id);
//        actCenterCompanyProductMapper.updateByPrimaryKey(actCenterCompanyProduct);
        actCenterCompanyProductMapper.deleteByPrimaryKey(id);
        return new Response<String>(ResultEnum.SUCCESS);
    }


    @Override
    public LayTableResponse<CenterCompanyProduct> selectData(Integer productId, Integer companyId, Integer roleId,Date timeFrom,Date timeTo, Integer start, Integer pageSize) {
        PageHelper.startPage(start, pageSize);
        List<CenterCompanyProduct> list = actCenterCompanyProductMapper.selectData(productId, companyId, roleId, timeFrom, timeTo, start, pageSize);
        return new LayTableResponse<CenterCompanyProduct>(list);
    }
}
